#!/bin/bash
#
# log2syslog          Start/Stop the log2syslog daemon.
#
# chkconfig: 2345 90 60
# description:log2syslog transfer log file to a remote syslog
# processname: log2syslog
# config: /etc/log2syslog.conf
# pidfile: /var/run/log2syslog.pid

# Source function library.
. /etc/init.d/functions

PIDSPATH=/var/run
SRVNAME=log2syslog
CONFIG_FILE=/etc/log2syslog.conf

execprog="/usr/bin/perl /usr/sbin/log2syslog $CONFIG_FILE"
lockfile=/var/lock/subsys/$SRVNAME

start() {
	RETVAL=1
	echo -n "Starting $SRVNAME : "
        if [ ! -f "$PIDSPATH/$SRVNAME.pid" ]; then
		$execprog >/dev/null 2>&1 &
		RETVAL=$?
		PROCPID=$!
		echo "$PROCPID" >> $PIDSPATH/$SRVNAME.pid
    	else
		echo -n " Already Running "
	fi
        
	if [ $RETVAL -eq 0 ]; then
		touch $lockfile
		success
	else
		failure
	fi
	echo 
	return $RETVAL
}
	
stop() {
        echo -n $"Shutting down $SRVNAME: "
	RETVAL=1
	if [ -f "$PIDSPATH/$SRVNAME.pid" ]; then
		cat "$PIDSPATH/$SRVNAME.pid" | xargs -n1 kill -9
		RETVAL=$?
		rm -f "$PIDSPATH/$SRVNAME.pid"
		[ $RETVAL -eq 0 ] && rm -f $lockfile
		success
	fi
	echo
	
	return $RETVAL
}

status() {
	STATUS=0
	[ -f "$PIDSPATH/$SRVNAME.pid" ] && STATUS=$(ps -edf | grep $SRVNAME | awk '{print $2}' | grep -c `cat $PIDSPATH/$SRVNAME.pid`)

	echo -n "STATUS de $SRVNAME : ["
	if [ "$STATUS" -eq "1" ]; then
		echo -ne '\E[30;47mRUNNING'
	else
		echo -ne '\E[31;47mSTOPPED'
	fi
	tput sgr0
	echo "]"
}
# See how we were called.
case "$1" in
  start)
	start
        ;;
  stop)
	stop
        ;;
  status)
	status
	RETVAL=$?
	;;
  restart|reload)
	stop
	start
	RETVAL=$?
	;;
  *)
        echo $"Usage: $0 {start|stop|restart|status}"
        RETVAL=3
esac

exit $RETVAL
